using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace DAL
{
	public static partial class NewsTypeService
	{
        public static bool AddNewsType(NewsType newsType)
		{
            string sql =
				"INSERT NewsType (Title)" +
				"VALUES (@Title)";
				
			sql += " ; SELECT @@IDENTITY";

            
			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@Title", newsType.Title)
			};
			
            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
			if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            
		}
		
		
        public static bool DeleteNewsTypeByID(int iD)
		{
            string sql = "DELETE NewsType WHERE ID = @ID";

           	SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", iD)
			};
		
            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            
		}
		
        public static bool ModifyNewsType(NewsType newsType)
        {
            string sql =
                "UPDATE NewsType " +
                "SET " +
	                "Title = @Title " +
                "WHERE ID = @ID";

			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", newsType.ID),
				new SqlParameter("@Title", newsType.Title)
			};

			int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		

        public static IList<NewsType> GetAllNewsTypes()
        {
            string sqlAll = "SELECT * FROM NewsType";
			return GetNewsTypesBySql( sqlAll );
        }
		
        public static NewsType GetNewsTypeByID(int iD)
        {
            string sql = "SELECT * FROM NewsType WHERE ID = @ID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", iD)
			};
			
            DataTable temp = DBHelper.ExecuteGetDataTable(CommandType.Text, sql, para);
			
			NewsType newsType = new NewsType();
			
            foreach (DataRow dr in temp.Rows)
            {
				newsType.ID = (int)dr["ID"];
				newsType.Title = (string)dr["Title"];
				
            }
            return newsType;    
            
        }
		
		
        private static IList<NewsType> GetNewsTypesBySql(string safeSql)
        {
            List<NewsType> list = new List<NewsType>();

			using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, null))
			{
				while (dr.Read())
				{
					NewsType newsType = new NewsType();
					
					newsType.ID = (int)dr["ID"];
					newsType.Title = (string)dr["Title"];
	
					list.Add(newsType);
				}
			}
            return list;
        }
		
        private static IList<NewsType> GetNewsTypesBySql(string safeSql, params SqlParameter[] para)
        {
            List<NewsType> list = new List<NewsType>();

			using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, para))
			{
				while (dr.Read())
				{
					NewsType newsType = new NewsType();
					
					newsType.ID = (int)dr["ID"];
					newsType.Title = (string)dr["Title"];
	
					list.Add(newsType);
				}
			}
            return list;
        }
	}
}
